﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace TbShop.Web.Helpers
{
    public static class UnityHelper
    {
        private static char[] constant =   
        {   
            '0','1','2','3','4','5','6','7','8','9',  
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
        };

        private static Random rd = new Random();
        public static string GetRandomNumber(int Length = 8)
        {
            lock (rd)
            {
                System.Text.StringBuilder newRandom = new System.Text.StringBuilder();
                for (int i = 0; i < Length; i++)
                {
                    newRandom.Append(constant[rd.Next(constant.Length)]);
                }
                return newRandom.ToString() + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            }
        }

        public static string GetRandom(int Length = 8, bool isNumber = true)
        {
            var count = isNumber ? 10 : constant.Count();
            lock (rd)
            {
                System.Text.StringBuilder newRandom = new System.Text.StringBuilder();
                for (int i = 0; i < Length; i++)
                {
                    newRandom.Append(constant[rd.Next(count)]);
                }
                return newRandom.ToString();
            }
        }

        public static string GenerateNonceStr()
        {
            return Guid.NewGuid().ToString().Replace("-", "");
        }

        public static string GetThumb(string fileName)
        {
            var str = fileName.Split('.');
            return "." + (str.Length > 1 ? str[1] : "jpg");
        }

        public static int ConvertDateTimeInt(DateTime time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// 验证文件流是否为图片文件
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static bool CheckDefaultImageStream(Stream stream)
        {
            var ret = true;
            try
            {
                var img = System.Drawing.Image.FromStream(stream);
                img.Dispose();
                stream.Seek(0, SeekOrigin.Begin);
            }
            catch
            {
                ret = false;
            }
            return ret;
        }


        public static string GetMD5(string value)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] data = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(value));
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sb.Append(data[i].ToString("x2"));
            }
            return sb.ToString();
        }

        public static string MaskPhoneNumber(this string phone)
        {
            try
            {
                var firstPart = phone.Substring(0, 3);
                var lastPart = phone.Substring(7);
                return firstPart + "****" + lastPart;
            }
            catch
            {
                return "手机号格式不正确";
            }
        }
    }
}